const router = require('koa-router')();
const ts = require('typescript');
const {
  resolve
} = require('url');
const tsCode = `
 class Scope {
  count: number = 0;

  handleClick(e: Event) {
    console.log('scope this: ', this);
    this.count += 1;
    this.title = '你好' + this.count;
  }
}
`;
router.get('/typescript/test', async function (ctx) {
  // const program = ts.createProgram([resolve(__dirname, './Scope.ts')], {});
  // let checker = program.getTypeChecker();
  //   const sourceFile = ts.createSourceFile(
  //     "Scope.ts",                  // filePath
  //     tsCode, // fileText
  //     ts.ScriptTarget.Latest,     // scriptTarget
  //     true                        // setParentNodes -- sets the `parent` property
  // );

  var fn = "tst.ts"
  var cd = `export interface IconIntf {
    name: IconName;
    data: string;
  }`;
  var o = ts.createSourceFile(fn, tsCode, ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS);
  //注意第三个参数,把那个参数整成false, 就可以避免Circular, 避免了Circular ,ts本身的JSON 序列化就可以用了
  var ast = JSON.parse(JSON.stringify(o))

  ctx.body = ast;
});

router.get('/typescript/test1', async function (ctx) {

  var parse = require('@babel/parser').parse
  // var tstyp = require('@babel/types')
  var ast = parse(tsCode, {
    sourceType: 'module',
    ranges: false,
    plugins: ['typescript']
  })

  ctx.body = ast;
});

module.exports = router